/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package itb.nlp.postagger.hmm;

import gate.Annotation;
import gate.AnnotationSet;
import gate.Document;
import gate.ProcessingResource;
import gate.Utils;
import gate.creole.ANNIEConstants;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.ExecutionException;
import gate.util.OffsetComparator;
import itb.nlp.postagger.hmm.decoder.MainTagger;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author asatrya
 */
public class HmmPosTagger extends AbstractLanguageAnalyser implements ProcessingResource {

    private String inputASname, outputASname;
    private URL fileLexicon, fileNGram, fileDic, fileCatTable;

    public String getinputASname() {
        return inputASname;
    }

    public void setinputASname(String inputASname) {
        this.inputASname = inputASname;
    }

    public String getoutputASname() {
        return outputASname;
    }

    public void setoutputASname(String outputASname) {
        this.outputASname = outputASname;
    }

    public URL getfileLexicon() {
        return fileLexicon;
    }

    public void setfileLexicon(URL fileLexicon) {
        this.fileLexicon = fileLexicon;
    }

    public URL getfileNGram() {
        return fileNGram;
    }

    public void setfileNGram(URL fileNGram) {
        this.fileNGram = fileNGram;
    }

    public URL getfileCatTable() {
        return fileCatTable;
    }

    public void setfileCatTable(URL fileCatTable) {
        this.fileCatTable = fileCatTable;
    }

    public URL getfileDic() {
        return fileDic;
    }

    public void setfileDic(URL fileDic) {
        this.fileDic = fileDic;
    }

    @Override
    public void execute() throws ExecutionException {
        Document doc = getDocument();

        doc.getFeatures().clear();

        AnnotationSet inputAnnSet = (inputASname == null || inputASname.length() == 0)
                ? doc.getAnnotations()
                : doc.getAnnotations(inputASname);

        AnnotationSet outputAnnSet = (outputASname == null || outputASname.length() == 0)
                ? doc.getAnnotations()
                : doc.getAnnotations(outputASname);

        doc.getFeatures().put("Number of character(s))",
                new Integer(doc.getContent().toString().length()).toString());

        List<Annotation> sentenceSet = new ArrayList<Annotation>(inputAnnSet.get(ANNIEConstants.SENTENCE_ANNOTATION_TYPE));
        AnnotationSet tokenSet = inputAnnSet.get(ANNIEConstants.TOKEN_ANNOTATION_TYPE);
        Collections.sort(sentenceSet, new OffsetComparator());

        MainTagger mt = new MainTagger(fileLexicon.toExternalForm(), fileNGram.toExternalForm(),
                fileCatTable.toExternalForm(), fileDic.toExternalForm(), 1);

        ListIterator<Annotation> sentenceIterator = sentenceSet.listIterator();
        while (sentenceIterator.hasNext()) {
            Annotation tSentence = sentenceIterator.next();
            List<Annotation> inSentenceToken = new ArrayList<Annotation>(tokenSet.getContained(Utils.start(tSentence), Utils.end(tSentence)));
            Collections.sort(inSentenceToken, new OffsetComparator());

            ListIterator<Annotation> inSentenceTokenIterator = inSentenceToken.listIterator();
            List<String> posTokenList = new ArrayList<String>();
            while (inSentenceTokenIterator.hasNext()) {
                Annotation tToken = inSentenceTokenIterator.next();
                posTokenList.add(Utils.contentFor(doc, tToken).toString());
            }
            List<String> tags = mt.taggingToken(posTokenList);

            ListIterator<String> tagsIterator = tags.listIterator();
            inSentenceTokenIterator = inSentenceToken.listIterator();
            while (inSentenceTokenIterator.hasNext()) {
                Annotation tToken = inSentenceTokenIterator.next();
                String tTag = tagsIterator.next();
                tToken.getFeatures().put("POS", tTag);
            }
        }

    }
}
